package com.kly.test.function

import android.app.Activity
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import androidx.core.view.size
import com.airbnb.lottie.LottieAnimationView
import com.google.android.material.bottomnavigation.BottomNavigationItemView
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.kly.test.R
import com.kly.test.databinding.ViewNavigationNumBinding

object NavigationHelper {
    fun init(activity : Activity, navView: BottomNavigationView) {
        val viewGroup = navView.getChildAt(0) as ViewGroup
        for (index in 0..< viewGroup.size) {
            val tabView =  viewGroup.getChildAt(index)
            val navigationViewItem = tabView as BottomNavigationItemView
            val viewNavigationNumBinding = ViewNavigationNumBinding.inflate(LayoutInflater.from(activity))
            navigationViewItem.addView(viewNavigationNumBinding.root,0)
            viewNavigationNumBinding.tvTitle.setText(getNavigationTitle().get(index))
            if (navigationViewItem.isSelected){
                viewNavigationNumBinding.tabAnimation.playAnimation()
                viewNavigationNumBinding.tvTitle.setTextColor(activity.getColor(R.color.purple_200))
            }else{
                viewNavigationNumBinding.tvTitle.setTextColor(activity.getColor(R.color.purple_500))
                viewNavigationNumBinding.tabAnimation.pauseAnimation()
            }
            viewNavigationNumBinding.navigationItem.clickListener {
                viewNavigationNumBinding.tabAnimation.clickListener(navView)
            }
        }
    }
    private fun getNavigationTitle(): ArrayList<String> {
        val title = arrayListOf<String>()
        title.add("列表")
        title.add("网络")
        title.add("空")
        return title
    }
    private fun View.clickListener(event: () -> Unit) {
        this.setOnTouchListener(object : View.OnTouchListener {
            var x: Int = 0
            var y: Int = 0
            var nowX: Int = 0
            var nowY: Int = 0
            override fun onTouch(p0: View?, p1: MotionEvent?): Boolean {
                when (p1?.action) {
                    MotionEvent.ACTION_DOWN -> {
                        x = p1.x.toInt()
                        y = p1.y.toInt()
                    }

                    MotionEvent.ACTION_MOVE -> {
                        nowX = p1.x.toInt()
                        nowY = p1.y.toInt()
                    }

                    MotionEvent.ACTION_UP -> {
                        if (Math.abs(nowX - x) < 10 || Math.abs(nowY - y) < 10) {
                            //点击事件
                            event.invoke()
                        }
                    }
                }
                event.invoke()
                return false
            }

        })
    }

    private fun LottieAnimationView.clickListener(bottomNavigationView: BottomNavigationView) {
        this.visibility = View.VISIBLE
        this.postDelayed({this.cancelAnimation()},500)
        this.playAnimation()
        val viewGroup = bottomNavigationView.getChildAt(0) as ViewGroup
        for (index in 0..< viewGroup.childCount){
            val tabView =  viewGroup.getChildAt(index)
            val navigationViewItem = tabView as BottomNavigationItemView
            val view = navigationViewItem.getChildAt(0)
            val viewNavigationNumBinding = ViewNavigationNumBinding.bind(view)
            if (this != viewNavigationNumBinding.tabAnimation){
                viewNavigationNumBinding.tabAnimation.visibility = View.GONE
                viewNavigationNumBinding.tvTitle.setTextColor(this.context.getColor(R.color.purple_500))
            }else{
                viewNavigationNumBinding.tvTitle.setTextColor(this.context.getColor(R.color.purple_200))
            }
        }
    }
}